﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Proj;
using ESOL.Business.Sales;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainMBOMQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region vars
        private ProdPlanOrderEntity mSelectedPlanOrder;
        private MstProductEntity mSelectedProduct;
        #endregion

        #region ctors
         public frmMaintainMBOMQuery()
        {
            InitializeComponent();
        }

         public frmMaintainMBOMQuery(ProdPlanOrderEntity entity)
        {
            InitializeComponent();
            mSelectedPlanOrder = entity;
        }
        #endregion

        #region functions
         private void BindRepositoryData()
         {
             List<MstCommonItemEntity> list1 = MstCommonItemBs.GetMstCommonItemListByType("PDTYPE");
             repositoryItemLookUpEdit1.DataSource = list1;
             repositoryItemLookUpEdit1.ValueMember = "ItemCode";
             repositoryItemLookUpEdit1.DisplayMember = "ItemName";
             List<MstCommonItemEntity> list2 = MstCommonItemBs.GetMstCommonItemListByType("BOM_S");
             repositoryItemLookUpEdit2.DataSource = list2;
             repositoryItemLookUpEdit2.ValueMember = "ItemCode";
             repositoryItemLookUpEdit2.DisplayMember = "ItemName";
             DataTable dt = new DataTable();
             dt.Columns.Add("name");
             dt.Columns.Add("value", typeof(int));
             dt.Rows.Add("否", 0);
             dt.Rows.Add("是", 1);
             repositoryItemLookUpEdit3.DataSource = dt;
             repositoryItemLookUpEdit3.ValueMember = "value";
             repositoryItemLookUpEdit3.DisplayMember = "name";
         }

         private void SetField()
         {
             if (mSelectedPlanOrder != null)
             {
                 txtProductPlanOrder.Text = mSelectedPlanOrder.PlanOrderCode;
                 SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mSelectedPlanOrder.ApplicationItemId);
                 mSelectedProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
                 txtProductName.Text = mSelectedProduct.ProductName;
                 txtProductCode.Text = mSelectedProduct.ProductCode;
             }
         }

         private void LoadBomByBomMaster()
         {
             if (mSelectedProduct != null)
             {
                 DataTable dt = ProjMBOMBs.GetDtProductBomByProductID(mSelectedProduct.ProductId, "PDTYPE01", "");
                 tlMain.DataSource = dt;
             }
         }
        #endregion

        #region events
         protected override void OnShown(EventArgs e)
         {
             base.OnShown(e);
             BindRepositoryData();
             SetField();
             LoadBomByBomMaster();
             tlMain.ExpandAll();
         }

         protected override void Click_Confirm()
         {
             //base.Click_Confirm();
             if (SelectedProductBomMaster == null)
             {
                 MessageBox.Show("请选择物料！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                 return;
             }
            DialogResult = DialogResult.OK;
         }

         private string sSelectedBomID;
         private DataRow drSelectedNode;
         public BomProductBomMasterEntity SelectedProductBomMaster { set; get; }
         private void tlMain_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
         {
             DataTable dt = (DataTable)tlMain.DataSource;
             sSelectedBomID = e.Node.GetValue("BOM_ID").ToString();
             drSelectedNode = dt.Select("BOM_ID='" + sSelectedBomID + "'")[0];
             SelectedProductBomMaster = new BomProductBomMasterEntity(drSelectedNode);
         }

         private void tlMain_DoubleClick(object sender, EventArgs e)
         {
             if (SelectedProductBomMaster == null)
             {
                 MessageBox.Show("请选择物料！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 return;
             }
             DialogResult = DialogResult.OK;
         }
        #endregion

    }
}
